<?php 
namespace App\Utils;
use App\Exceptions\BaseException;
use Excel;
class ExportExcel
{

    public function export($tableName, $data)
    {   
        if(count($data) < 1){
            throw new BaseException('exceptions.system', '请选择导出数据');
        }

        $tableName = $tableName.'-'.strftime('%Y-%m-%d %H-%M-%S');

        Excel::create($tableName, function($excel) use ($data) {
            
            $excel->sheet('First sheet', function($sheet) use ($data) {

                $sheet->setFontSize(12);

                $sheet->setWidth(array(
                    'A'     =>  6,
                    'B'     =>  14,
                    'C'     =>  24,
                    'D'     =>  18,
                    'E'     =>  14,
                    'F'     =>  14,
                    'G'     =>  14,
                    'H'     =>  18,
                    'I'     =>  18,
                    'J'     =>  14,
                    'K'     =>  20,
                    'L'     =>  30,
                    'M'     =>  20,
                ));

                $sheet->freezeFirstRow();

                $sheet->cell('A:M',function($cells) {
                    $cells->setAlignment('center');
                });

                 $sheet->cell('A1:M1',function($cells) {
                    $cells->setFontFamily('Calibri');
                    // $cells->setFontColor('#ffffff');
                    // $cells->setBackground('#e78012');
                });

                $sheet->setBorder('A1:M1', 'thin');

                $sheet->fromArray($data);

            });
        })->export('xls');
    }

}
